<?php
require_once 'common.inc.php';

$children  = array();
$category  = pdo_fetchall("SELECT * FROM " . tablename(MODULE_PREFIX.'category') . " WHERE uniacid = '{$_W['uniacid']}' and deleted = 0 ORDER BY parentid ASC, displayorder DESC");
foreach ($category as $index => $row) {
    if (!empty($row['parentid'])) {
        $children[$row['parentid']][] = $row;
        unset($category[$index]);
    }
}
if ($operation == 'display'){
    if (!empty($_GPC['datas'])) {
        $datas = json_decode(html_entity_decode($_GPC['datas']), true);
        if (!is_array($datas)) {
            message('分类保存失败，请重试!', '', 'error');
        }
        $cateids      = array();
        $displayorder = count($datas);
        foreach ($datas as $row) {
            $cateids[] = $row['id'];
            pdo_update(MODULE_PREFIX.'category', array( 'parentid' => 0, 'displayorder' => $displayorder, 'level' => 1 ), array( 'id' => $row['id'] ));
            if ($row['children'] && is_array($row['children'])) {
                $displayorder_child = count($row['children']);
                foreach ($row['children'] as $child) {
                    $cateids[] = $child['id'];
                    pdo_query('update ' . tablename(MODULE_PREFIX.'category') . ' set  parentid=:parentid,displayorder=:displayorder,level=2 where id=:id', array(':displayorder' => $displayorder_child, ":parentid" => $row['id'], ":id" => $child['id'] ));
                    $displayorder_child--;
                }
            }
            $displayorder--;
        }
        if (!empty($cateids)) {
            pdo_query('delete from ' . tablename(MODULE_PREFIX.'category') . ' where id not in (' . implode(',', $cateids) . ') and uniacid=:uniacid', array(
                ':uniacid' => $_W['uniacid']
            ));
        }
        message('分类更新成功！', $this->createWebUrl('category', array( 'op' => 'display' )), 'success');
    }
}

elseif ($operation == 'post'){
    $parentid = intval($_GPC['parentid']);
    $id       = intval($_GPC['id']);
    
    if (!empty($id)) {
        $item     = pdo_fetch("SELECT * FROM " . tablename(MODULE_PREFIX.'category') . " WHERE id = '$id' limit 1");
        $parentid = $item['parentid'];
    } else {
        $item = array(
            'displayorder' => 0
        );
    }
    
    if (!empty($parentid)) {
        $parent = pdo_fetch("SELECT id, parentid, name FROM " . tablename(MODULE_PREFIX.'category') . " WHERE id = '$parentid' limit 1");
        if (empty($parent)) {
            message('抱歉，上级分类不存在或是已经被删除！', $this->createWebUrl('post'), 'error');
        }
    }
    if (empty($parent)) {
        $level = 1;
    } else {
        $level = 2;
    }
    if (checksubmit('submit')) {
        if (empty($_GPC['catename'])) {
            message('抱歉，请输入分类名称！');
        }
        $data = array(
            'uniacid' => $_W['uniacid'],
            'name' => trim($_GPC['catename']),
            'enabled' => intval($_GPC['enabled']),
            'displayorder' => intval($_GPC['displayorder']),
            'description' => $_GPC['description'],
            'parentid' => intval($parentid),
            'thumb' => $_GPC['thumb'],
            'level' => $level
        );
        if (!empty($id)) {
            unset($data['parentid']);
            pdo_update(MODULE_PREFIX.'category', $data, array('id' => $id ));
            load()->func('file');
            file_delete($_GPC['thumb_old']);
        } else {
            pdo_insert(MODULE_PREFIX.'category', $data);
            $id = pdo_insertid();
        }
        message('更新分类成功！', $this->createWebUrl('category', array('op' => 'display' )), 'success');
    }
}

elseif($operation == 'delete'){
    $id = intval($_GPC['id']);
    $item = pdo_fetchcolumn("select count(1) from ".tablename(MODULE_PREFIX."category")." where uniacid=:uniacid and id=:id ",array(':uniacid'=>$uniacid,':id'=>$id));
    if(empty($item)){
        message("当前分类不存在或已删除！",referer(),"error");
    }
    pdo_update(MODULE_PREFIX."category",array('deleted'=>1),array('id'=>$id));
    
    message("分类删除成功！",referer(),"success");
}

include $this->template('web/category');